/*
 * @Author: xuetao 3168581151@qq.com
 * @Date: 2025-01-28
 * @LastEditors: xuetao 3168581151@qq.com
 * @LastEditTime: 2025-02-08
 * @Description: 申请模型
 */
import 'package:hive/hive.dart';

part 'apply.g.dart';

@HiveType(typeId: 5)
class Apply {
  @HiveField(0)
  final bool isSend; // 是否为自己发出的申请

  @HiveField(1)
  final String opponentId; // 申请的另一方的ID

  @HiveField(2)
  final String opponentNikename; // 申请的另一方的昵称

  @HiveField(3)
  final String opponentAvatarUrl; // 申请的另一方的头像

  @HiveField(4)
  final String applyInfo; //申请信息

  @HiveField(5)
  late int status; // 状态 0:待处理 1:已经是好友 2:申请记录已失效（包括拒绝和已添加的好友删除）

  @HiveField(6)
  final DateTime createdAt; // 申请时间

  Apply({
    required this.isSend,
    required this.opponentId,
    required this.opponentNikename,
    required this.opponentAvatarUrl,
    required this.applyInfo,
    required this.status,
    required this.createdAt,
  });

  // 工厂方法，用于从 JSON 数据创建 Apply 实例
  factory Apply.fromJson(Map<String, dynamic> json, bool isSend) {
    return Apply(
      isSend: isSend,
      opponentId: json[isSend ? 'receiver' : 'sender']['_id'] ?? '',
      opponentNikename: json[isSend ? 'receiver' : 'sender']['nickname'] ?? '',
      opponentAvatarUrl:
          json[isSend ? 'receiver' : 'sender']['avatarUrl'] ?? '',
      applyInfo: json['applyInfo'] ?? '',
      status: json['status'] ?? 0,
      createdAt:
          DateTime.parse(json['createdAt'] ?? DateTime.now().toIso8601String()),
    );
  }

  // 静态方法，用于从 JSON 数据创建 Apply 列表
  static List<Apply> fromJsonList(Map<String, dynamic> json) {
    List<Apply> applyList = [];
    if (json['receiverList'] != null && json['receiverList'].isNotEmpty) {
      for (var item in json['receiverList']) {
        applyList.add(Apply.fromJson(item, false));
      }
    }

    if (json['senderList'] != null && json['senderList'].isNotEmpty) {
      for (var item in json['senderList']) {
        applyList.add(Apply.fromJson(item, true));
      }
    }
    return applyList;
  }

  // 重写 toString 方法
  @override
  String toString() {
    return 'Apply(isSend: $isSend, opponentId: $opponentId, opponentNikename: $opponentNikename, opponentAvatarUrl: $opponentAvatarUrl, applyInfo: $applyInfo, status: $status, createdAt: $createdAt)';
  }
}

enum ApplyStatus {
  pending, // 等待验证
  accepted, // 已接受
  rejected, // 已拒绝
  overdue, // 已过期
}

extension ApplyStatusExtension on ApplyStatus {
  static ApplyStatus fromInt(int value) {
    switch (value) {
      case 0:
        return ApplyStatus.pending;
      case 1:
        return ApplyStatus.accepted;
      case 2:
        return ApplyStatus.rejected;
      case 3:
        return ApplyStatus.overdue;
      default:
        throw ArgumentError('Invalid status value');
    }
  }

  String get label {
    switch (this) {
      case ApplyStatus.pending:
        return '等待验证';
      case ApplyStatus.accepted:
        return '已接受';
      case ApplyStatus.rejected:
        return '已拒绝';
      case ApplyStatus.overdue:
        return '已过期';
    }
  }

  int get value {
    switch (this) {
      case ApplyStatus.pending:
        return 0;
      case ApplyStatus.accepted:
        return 1;
      case ApplyStatus.rejected:
        return 2;
      case ApplyStatus.overdue:
        return 3;
    }
  }
}
